package com.lzq.chatroom_project.controller;

import com.lzq.chatroom_project.common.pojo.Result;
import com.lzq.chatroom_project.common.pojo.ResultCode;
import com.lzq.chatroom_project.model.Message;
import com.lzq.chatroom_project.model.User;
import com.lzq.chatroom_project.service.BitmapService;
import com.lzq.chatroom_project.service.impl.MessageServiceImpl;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RequestMapping("/message")
@RestController
public class MessageController {

    @Autowired
    private MessageServiceImpl messageService;

    /**
     * 获取消息列表
     * @param sessionId
     * @return Result(messageList)
     */
    @GetMapping("/getMessageList")
    public Result getMessageList(Integer sessionId, HttpSession session) {
        log.info("开始执行getMessageList方法,接收参数:sessionId:{}",sessionId);
        User user = (User)session.getAttribute("user");
        if (user == null || user.getUserId() <= 0) {
            log.error("查询到的用户user:{}",user);
            return Result.failed(ResultCode.FAILED);
        }
        List<Message> messageList = messageService.getMessageList(sessionId, user.getUserId());
        return Result.success(messageList);
    }

    @GetMapping("/getHistoryMessageByDate")
    public Result getHistoryMessageByDate(Integer sessionId, HttpSession session, LocalDate date) {
        log.info("开始执行getHistoryMessageByDate方法," +
                "接收参数:sessionId:{}, date:{}",sessionId, date);
        User user = (User)session.getAttribute("user");
        if (user == null || user.getUserId() <= 0) {
            log.error("查询到的用户user:{}",user);
            return Result.failed(ResultCode.FAILED);
        }
        List<Message> messageList = messageService.getHistoryMessageByDate(sessionId, date);
        return Result.success(messageList);
    }

    @GetMapping("/getHistoryMessageByKeyword")
    public Result getHistoryMessageByKeyword(Integer sessionId, HttpSession session, String keyword) {
        log.info("开始执行getHistoryMessageByKeyword方法," +
                "接收参数:sessionId:{}, keyword:{}",sessionId, keyword);
        User user = (User)session.getAttribute("user");
        if (user == null || user.getUserId() <= 0) {
            log.error("查询到的用户user:{}",user);
            return Result.failed(ResultCode.FAILED);
        }
        List<Message> messageList = messageService.getHistoryMessageByKeyword(sessionId, keyword);
        return Result.success(messageList);
    }

}
